package com.emm.yixun.mobile.ui.housing;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.emm.yixun.mobile.R;
import com.emm.yixun.mobile.adapter.BuildingStatusListAdapter;
import com.emm.yixun.mobile.adapter.MarKetingAdapter;
import com.emm.yixun.mobile.application.EmmApplication;
import com.emm.yixun.mobile.base.BaseActivity;
import com.emm.yixun.mobile.model.GetMarketingControlList;
import com.eroad.product.tools.MyListView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import java.util.HashMap;
import java.util.Map;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import xf.tools.Loading;

/**
 * 
 * <p>
 * <b>Title：</b> 销控
 * </p>
 * <p>
 * <b>Description：</b>
 * </p>
 * 
 * @author tangxin
 * @date 2016年12月6日 下午4:11:06
 */
public class MarketingControlActivity extends SwipeBackActivity {

	private final static String TAG = "MarketingControlActivity";

	/** 楼栋Id */
	private String buildingId = "";

	/** 组团ID */
	private String premisesId = "";

	/** back按钮 */
	ImageView back_btn;

	/** 标题 */
	TextView title_main;

	/** 项目小区楼栋布局 */
	RelativeLayout user_linean1;

	/** 是否展开 */
	private boolean IsOpenTop = true;

	/** 是否需要展开的布局 */
	LinearLayout top_linear;

	/** 箭头 */
	ImageView rl7;

	GridView gridview_marketing;

	BuildingStatusListAdapter T1adapter;

	MyListView marketing_list;

	MarKetingAdapter T2adapter;

	TextView name_title;

	private String buildingName = "";

	private String premisesNam = "";

	/** 侧滑关闭框架 */
	private SwipeBackLayout mSwipeBackLayout;
	
	@Override
	protected void onResume() {
		BaseActivity.activity = this; // TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);

		setContentView(R.layout.marketing_control_activity);
		
		SetSwipeBackLayout(mSwipeBackLayout);
		
		BaseActivity.activity = this;

		buildingId = getIntent().getStringExtra("buildingId");
		premisesId = getIntent().getStringExtra("premisesId");
		premisesNam = getIntent().getStringExtra("premisesName");
		buildingName = getIntent().getStringExtra("buildingName");

		Log.v(TAG, "buildingId:" + buildingId + "----------premisesId:" + premisesId);

		initView();
		
		SetRelativiLayoutPadding(R.id.main_head_top_rela);
		
		initBtn();
		GetMarketingControlList();
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化view
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午3:35:28
	 */
	private void initView() {
		back_btn = (ImageView) findViewById(R.id.back_btn);
		title_main = (TextView) findViewById(R.id.title_main);
		title_main.setText(EmmApplication.getData(EmmApplication.ProjectName_content));
		user_linean1 = (RelativeLayout) findViewById(R.id.user_linean1);
		top_linear = (LinearLayout) findViewById(R.id.top_linear);
		rl7 = (ImageView) findViewById(R.id.rl7);
		gridview_marketing = (GridView) findViewById(R.id.gridview_marketing);
		marketing_list = (MyListView) findViewById(R.id.marketing_list);
		name_title = (TextView) findViewById(R.id.name_title);
		name_title.setText(EmmApplication.getData(EmmApplication.ProjectName_content) + "-" + premisesNam + "-" + buildingName);
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 初始化监听事件
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午3:35:36
	 */
	private void initBtn() {
		back_btn.setVisibility(View.VISIBLE);
		back_btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		if (IsOpenTop) {
			top_linear.setVisibility(View.VISIBLE);
			rl7.setImageResource(R.drawable.j_bottom);
		} else {
			top_linear.setVisibility(View.GONE);
			rl7.setImageResource(R.drawable.j_top);
		}
		user_linean1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (IsOpenTop) {
					IsOpenTop = false;
					top_linear.setVisibility(View.GONE);
					rl7.setImageResource(R.drawable.j_top);
				} else {
					IsOpenTop = true;
					top_linear.setVisibility(View.VISIBLE);
					rl7.setImageResource(R.drawable.j_bottom);
				}
			}
		});

	}

	public static int position_ = 0;

	/**
	 * 查询销控列表接口
	 */
	GetMarketingControlList getmark;

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 查询销控列表接口
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @author tangxin
	 * @date 2016年12月7日 下午3:35:56
	 */
	private void GetMarketingControlList() {
		EmmApplication.updateUrl("getMarketingControlList");
		Map<String, String> map = new HashMap<String, String>();
		map.put("version", EmmApplication.getPackageManager(5));
		map.put("merchantId", EmmApplication.getData(EmmApplication.merchantId));
		map.put("token", EmmApplication.getToken());
		map.put("userId", EmmApplication.getUserId());
		map.put("projectId", EmmApplication.getProjectID());
		map.put("projectCode", EmmApplication.getProjectCode());
		map.put("premisesId", premisesId);
		map.put("buildingId", buildingId);
		map.put("terminalCode", EmmApplication.getPackageManager(2));
		Map<String, Object> paramsMap = new HashMap<String, Object>();
		paramsMap.put("params", map);
		JSONObject json = new JSONObject(paramsMap);
		String jsonstr = json.toString();
		RequestParams params = new RequestParams();
		params.put("body", jsonstr);
		Log.v("getMarketingControlList-->post", EmmApplication.urlStr + "?body=" + jsonstr);
		AsyncHttpClient asyncHttpClient = new AsyncHttpClient();
		asyncHttpClient.post(EmmApplication.urlStr, params, new AsyncHttpResponseHandler() {
			@Override
			public void onFailure(Throwable error, String content) {
				super.onFailure(error, content);
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束error:" + error + "==" + content);
			}

			@Override
			public void onFinish() {
				Loading.hideDialogForLoading();
				Log.v(TAG, "请求结束");
				super.onFinish();
			}

			@Override
			public void onStart() {
				super.onStart();
				Loading.showDialogForLoading(MarketingControlActivity.this, "正在加载...", false, true, 60000);
				Log.v(TAG, "请求开始");
			}

			@Override
			public void onSuccess(String content) {
				super.onSuccess(content);
				if (content == null) {
					return;
				}
				Log.v(TAG, content);
				JSONObject jsonObj = JSONObject.parseObject(content);
				JSONObject resData = (JSONObject) jsonObj.get("resData");
				if (resData == null) {
					Log.v(TAG, "信息返回值为空");
					return;
				}

				getmark = JSONObject.parseObject(resData.toString(), GetMarketingControlList.class);

				if ("1".equals(getmark.getResult())) {
					Log.v("获取成功", "");

					T1adapter = new BuildingStatusListAdapter(MarketingControlActivity.this, getmark.getBuildingStatusList());
					gridview_marketing.setAdapter(T1adapter);

					T2adapter = new MarKetingAdapter(MarketingControlActivity.this, getmark.getUnitList());
					marketing_list.setAdapter(T2adapter);

				} else {
					EmmApplication.T2(getmark.getErrorMsg().toString());
					Log.v("获取失败", "errorCode:" + getmark.getErrorCode().toString() + "errorMsg:" + getmark.getErrorMsg().toString());
				}
			}
		});
	}

	/**
	 * 
	 * <p>
	 * <b>Title：</b> 设置gridview排列样式
	 * </p>
	 * <p>
	 * <b>Description：</b>
	 * </p>
	 * 
	 * @param a
	 * @param gv
	 * @param length
	 * @param size
	 * @author tangxin
	 * @date 2016年12月7日 下午3:37:22
	 */
	public static void setGridView(Activity a, GridView gv, int length, int size) {
		DisplayMetrics dm = new DisplayMetrics();
		a.getWindowManager().getDefaultDisplay().getMetrics(dm);
		float density = dm.density;
		int gridviewWidth = (int) (size * (length + 4) * density);
		int itemWidth = (int) (length * density);
		LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(gridviewWidth, LinearLayout.LayoutParams.FILL_PARENT);
		gv.setLayoutParams(params); // 设置GirdView布局参数,横向布局的关键
		gv.setColumnWidth(itemWidth); // 设置列表项宽
		gv.setHorizontalSpacing(5); // 设置列表项水平间距
		gv.setVerticalSpacing(5);// 设置列表项垂直间距
		gv.setStretchMode(GridView.NO_STRETCH);
		gv.setNumColumns(size); // 设置列数量=列表集合数

	}
}